import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.sql.*;
import java.util.*;

public class FilmSystem extends JFrame {
	Connection connection;
	Scanner scanner;
	LinkedList<FilmData> data;
	final static int COLUMNS = 6;
	final String[] types_zh = {"动作片", "喜剧片", "科幻片"},
			types_en = {"action", "comedy", "sci-fi"};
	final Map<String, String> types_trans;

	FilmSystem(int port, String schema)
			throws Exception {
		int y = 0;
		load(port, schema);

		types_trans = new HashMap<>();
		for (int i = 0; i < types_zh.length; i++) {
			types_trans.put(types_zh[i], types_en[i]);
		}

		setLayout(new GridBagLayout());

		JLabel title = new JLabel("欢迎使用电影推荐系统");
		title.setFont(new Font("宋体", Font.BOLD, 0x20));
		add(title, myConstraints(1, y++));
		JLabel prompt = new JLabel("请选择你想观看的影片类型");
		prompt.setFont(new Font("宋体", Font.BOLD, 0x18));
		add(prompt, myConstraints(1, y++));

		for (String type_zh : types_zh) {
			add(new FilmTypeList(type_zh), myConstraints(1, y++));
		}

		setSize(0x400, 0x400);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		setVisible(true);
	}

	void load(int port, String schema) throws SQLException {
		connection = DriverManager.getConnection(
				"jdbc:MySQL://localhost:port/schema".
						replaceAll("port", String.valueOf(port))
						.replaceAll("schema", schema)
						+ "?useSSL=false&serverTimezone=UTC",
				"root", "A7btngqxA7btngqx"
		);
		data = new LinkedList<>();
	}

	GridBagConstraints myConstraints(int x, int y) {
		GridBagConstraints constraints = new GridBagConstraints();
		constraints.gridx = x;
		constraints.gridy = y;
		return constraints;
	}

	class FilmTypeList extends JButton {
		final String[] titles={"名称","类型","演员","评价","已评人数","年份"};
		public FilmTypeList() {
		}

		public FilmTypeList(String text) {
			super(text);
			addMouseListener(new MouseAdapter() {
				@Override
				public void mouseClicked(MouseEvent e) {
					try {
						String sql = "SELECT * FROM film_sys.data where type = ?";
						PreparedStatement statement = FilmSystem.this.connection.
								prepareStatement(sql);
						statement.setString(1, FilmSystem.this.
								types_trans.get(FilmTypeList.this.getText()));
						ResultSet set = statement.executeQuery();
						FilmSystem.this.data.clear();
						while (set.next()) {
							FilmSystem.this.data.add(new FilmData(
											set.getString(1), set.getString(2),
											set.getString(3),
											set.getFloat(4) / set.getInt(5),
											set.getInt(5), set.getInt(6)
									)
							);
						}
						JFrame dataFrame=new JFrame(FilmTypeList.this.getText());
						dataFrame.setLayout(new GridBagLayout());
						JTable filmData = (new MyTable(data,titles));
						dataFrame.add(new JScrollPane(filmData)
								, FilmSystem.this.myConstraints(2, 2)
						);
						dataFrame.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
						dataFrame.setSize(0x400,0x400);
						dataFrame.setVisible(true);
					} catch (Exception exception) {
						JOptionPane.showMessageDialog(
								FilmTypeList.this,
								exception.getCause());
					}
				}
			});
		}
	}
}
